/**
 * AutoDJ vids
 */
var autodj = {
	
	enabled : false,
	requesting : false,
	initialized : false,
	source : 'auto',
	
	enable : function() {
		debug.print("autodj.enable");
		if (!this.isEnabled()) {
			autoplay.enable();
			this.enabled = true;
			ui.setAutodj(true);
		}
	},
	
	disable : function() {
		debug.print("autodj.disable");
		autoplay.disable();
		this.enabled = false;
		this.initialized = false;
		ui.setAutodj(false);
	},
	
	isEnabled : function() {
		debug.print("autodj.isEnabled");
		return this.enabled;
	},
	
	check : function() {
		debug.print("autodj.check");
		if (autodj.isEnabled() === true && autodj.requesting === false) {
			if (np.count() < 2) {
				if (autodj.source == 'search' && search_videos_list.count() > 0) {
					autodj.addSimilar(search_videos_list.getFirst().data("video"));
				} else if ((autodj.source == 'np' || (player[0].getState() != 1 && player[1].getState() != 1)) && np.count() > 0) {
					autodj.addSimilar(np.getLast().data("video"));
				} else if (player[0].getState() == 1) {
					autodj.addSimilar(player[0].video);
				} else if (player[1].getState() == 1) { 
					autodj.addSimilar(player[1].video);
				}									
			}
		}
	},
	
	addSimilar : function(item, callback) {
		debug.print("autodj.addSimilar");
		autodj.requesting = true;
		setTimeout(function() {
			autodj.requesting = false;
		}, 15000);
		var url = autodj.initialized ? "/autodj/getSimilar/json" : "/autodj/getSimilar/json?q=" + item.artist;  
		$.getJSON(url, function(data){
			var video = data.video;
			if (ObjectLength(video) == 0) {
				flash.flash("No similar", "artist found");
				autodj.disable();
				if (callback) {
					callback('error');
				}
			} else {
				np.add(video);
				autodj.initialized = true;
				if (callback) {
					callback('ok');
				}
			}
			autodj.requesting = false;
		});
	},
	
	showLabel : function(key) {
		debug.print("autodj.showLabel [key: "+key+"]");
		var lbl = $("#autodj-next" + key + "-label span");
		var div = $("#autodj-next" + key + "-label");
		if (np.count() > 0 && ui.nextDisabled[key] == false) {
			var video = np.getNext().data("video");
			lbl.text(video.artist + " - " + video.title);
			div.show().animate({ top : "115px", opacity: 1 });
		}		
	},
	
	hideLabel : function(key) {
		debug.print("autodj.hideLabel [key: "+key+"]");
		var lbl = $("#autodj-next" + key + "-label span");
		var div = $("#autodj-next" + key + "-label");
		div.animate({ top : "105px", opacity: 0 }, function() { $(this).hide(); });
	}
	
}
